"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RoomTemplateDao = void 0;
const DbConfig_1 = require("../../src/config/DbConfig");
const GameConfig_1 = require("../../src/config/GameConfig");
const RoomConfig_1 = require("../../src/config/RoomConfig");
const DaoBase_1 = require("../../src/engine/dao/DaoBase");
const DBSourceManager_1 = require("../../src/engine/utils/DBSourceManager");
const Logger_1 = require("../../src/engine/utils/Logger");
const MysqlManager_1 = require("../../src/engine/utils/MysqlManager");
const RoomTemplateEntity_1 = require("../entity/RoomTemplateEntity");
class RoomTemplateDao extends DaoBase_1.DaoBase {
    static getRepository() {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepositoryBase(RoomTemplateEntity_1.RoomTemplateEntity, this.getGameDbName());
            return repository;
        });
    }
    static insert_room_template(userid, protoPack) {
        return __awaiter(this, void 0, void 0, function* () {
            let entity = new RoomTemplateEntity_1.RoomTemplateEntity();
            entity.clubid = protoPack.clubid;
            entity.creator = userid;
            entity.game_type = protoPack.gameType;
            entity.name = protoPack.name;
            entity.roomType = protoPack.roomType;
            entity.conf_base = JSON.stringify(protoPack.roomConfigBase);
            if (entity.game_type == GameConfig_1.GameConfig.Game_Type.Mj) {
                entity.config_game = JSON.stringify(protoPack.roomConfigMj);
            }
            let repository = yield this.getRepository();
            let result = yield DBSourceManager_1.DBSourceManager.instance.insert(repository, entity);
            if (result && result.raw["affectedRows"] > 0) {
                return true;
            }
            return false;
        });
    }
    static get_all_room_template() {
        return __awaiter(this, void 0, void 0, function* () {
            let sql = "select * from  t_room_template ;";
            let repository = yield this.getRepository();
            let results = yield repository.query(sql);
            if (results && results.length) {
                return results;
            }
            return [];
        });
    }
    static delete_room_template(id) {
        return __awaiter(this, void 0, void 0, function* () {
            let sql = "delete from t_room_template where id=?;";
            let sqlParams = [id];
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let rows = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("delete_room_template==", sql, sqlParams, rows);
            if (rows && rows["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static get_ziyou_room_template(gameType) {
        return __awaiter(this, void 0, void 0, function* () {
            let sql = "select * from  t_room_template  where `game_type`=? and `roomType`=?;";
            let sqlParams = [gameType, RoomConfig_1.RoomConfig.Room_Type.Ziyou];
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.getGameDbName());
            let rows = yield client.queryPromise(sql, sqlParams);
            if (rows && rows.length > 0) {
                return rows;
            }
            return [];
        });
    }
}
exports.RoomTemplateDao = RoomTemplateDao;
RoomTemplateDao.DB_NAME = DbConfig_1.DbConfig.Db_Name.db_admin;
RoomTemplateDao.Tb_Name = "t_room_template";
